package example.step3;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamField;
import java.io.Serializable;

public class SerialFields implements Serializable {

    /**
     *
     */
    private static final long serialVersionUID = -8928285570197854374L;
    private int age = 27;
    private String name;
    private Integer iAge = 27;

    private static final ObjectStreamField[] serialPersistentFields = {
            new ObjectStreamField("age", int.class),
            new ObjectStreamField("iAge", Integer.class) };

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getiAge() {
        return iAge;
    }

    public void setiAge(Integer iAge) {
        this.iAge = iAge;
    }

    @Override
    public String toString() {
        return "SerialFields [age=" + age + ", name=" + name + ", iAge=" + iAge
                + "]";
    }

    // 运行函数
    public static void main(String args[]) throws Exception {
        ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
                "E:\\idea-workspace\\gitee\\demo\\serializable\\src\\main\\java\\objfile\\serialfields.obj"));
        // 序列化对象
        SerialFields person = new SerialFields();
        person.setAge(27);
        person.setName("Lang Yu");
        // 数据的序列化写入
        out.writeObject(person); // writeExternal 自动执行
        out.close();
        // 反序列化对象
        ObjectInputStream in = new ObjectInputStream(new FileInputStream(
                "E:\\idea-workspace\\gitee\\demo\\serializable\\src\\main\\java\\objfile\\serialfields.obj"));
        // 数据的反序列化读取
        SerialFields target = (SerialFields) in.readObject();
        System.out.println(target);
        in.close();
    }
}